import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.common.eventbus.SubscriberExceptionHandler;
import com.sun.istack.internal.NotNull;
import guava.TaskInstanceCancel;
import guava.TaskInstanceCancelSubscribe;

import java.util.concurrent.*;

public class A {
    public static void main(String[] args) throws InterruptedException {

        //basicHandler();
        relationEvent();
    }

    public static void basicHandler() throws InterruptedException {
        // 创建exception处理类;
        SubscriberExceptionHandler handler = new SubscriberExceptionHandler() {
            @Override
            public void handleException(Throwable exception, SubscriberExceptionContext context) {
                System.out.println(exception);
                // 重试;
                //context.getEventBus().post(context.getEvent());
            }
        };
        // 同步事件总线;
        EventBus eventBus = new EventBus(handler);
        // 注册监听器/订阅者;
        eventBus.register(new TaskInstanceCancelSubscribe());
        // 发送事件;
        for (int i = 0; i < 1024 * 10000; i++) {
            eventBus.post(1.1);
            eventBus.post("gc");
            eventBus.post("gc");
        }
        System.out.println("===");
        //TimeUnit.MINUTES.sleep(1);
    }

    public static void relationEvent() throws InterruptedException {
        // 创建线程工厂,设置线程名;
        ThreadFactory threadFactory = new ThreadFactory() {
            @Override
            public Thread newThread(@NotNull Runnable r) {
                Thread t = new Thread(r);
                t.setName("async");
                return t;
            }
        };
        Executor executor = new ThreadPoolExecutor(100, 100, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), threadFactory);
        // 异步事件总线;
        AsyncEventBus eventBus = new AsyncEventBus(executor);
        eventBus.register(new TaskInstanceCancelSubscribe());
        for (int i = 0; i < 1024 * 1000000; i++) {
            eventBus.post(new TaskInstanceCancel("dl", ""));
        }
        System.out.println("----");

    }
}
